/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package beans;

import entities.Restaurant;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.UserTransaction;

/**
 *
 * @author HP
 */
@ManagedBean (name = "newrestaurantbean")
@RequestScoped
public class NewRestaurantBean {

    @PersistenceContext 
    private EntityManager em;
    @Resource 
    private UserTransaction utx;
    private Restaurant restaurant = new Restaurant();
    
    public Restaurant getRestaurant() {
        return restaurant;
    }

    public void setRestaurant(Restaurant restaurant) {
        this.restaurant = restaurant;
    }

    public void createRestaurant() {
        FacesContext context = FacesContext.getCurrentInstance();
         if(restaurant.getPic()==null || restaurant.getPic().trim().equals(""))
                restaurant.setPic("defaultRestaurant.png");
        try {
            utx.begin();
            em.persist(restaurant);
            utx.commit();
            
        } catch (Exception e) {
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR,
                    "Error creating Restaurant!",
                    "Unexpected error when creating this restaurant.  Please contact the system Administrator");
            context.addMessage(null, message);
            Logger.getAnonymousLogger().log(Level.SEVERE,
                    "Unable to create new restaurant",
                    e);

        }
    }
}
